<?php

declare(strict_types=1);

namespace App\CustHub\Model;

use App\CustHub\Constants\QuestionnaireAnswerConstants;
use Mine\MineModel;

/**
 * @property int $id 主键
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @property int $questionnaire_id 
 * @property array $content 
 * @property string $question_type 
 * @property int $sort 
 */
class ChSmartQuestionnaireItem extends MineModel
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'ch_smart_questionnaire_item';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'created_at', 'updated_at', 'questionnaire_id', 'content', 'question_type', 'sort'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'questionnaire_id' => 'integer', 'content' => 'array', 'sort' => 'integer'];

    public function option()
    {
        return $this -> hasMany(ChSmartQuestionnaireOption::class,'item_id','id');
    }

    public static function saveItems($questionnaireId,$data)
    {
        $itemIds = [];
        foreach ($data as $key => $item)
        {
            $sort = $key + 1;
            if(!empty($item['id'])){
                $newItemModel = self::query() -> where('id',$item['id']) -> first();
                $newItemModel -> questionnaire_id = $questionnaireId;
                $newItemModel -> sort = $sort;
                $newItemModel -> content = $item['content'];
                $newItemModel -> question_type = $item['question_type'];
                $newItemModel -> save();
            }else{
                $newItemModel = self::query() -> updateOrCreate([
                    'questionnaire_id' => $questionnaireId,
                    'sort' => $sort,
                ],[
                    'content' => $item['content'],
                    'question_type' => $item['question_type'],
                ]);
            }
            $itemIds[] = $newItemModel -> id;
            if(in_array($item['question_type'],QuestionnaireAnswerConstants::getOptionTypes())){

                //选择题需要更新option
                $optionIds = [];
                $options = $item['option'];
                foreach ($options as $key1 => $option){
                    $sort1 = $key1 + 1;
                    $newOptionModel = ChSmartQuestionnaireOption::query() -> updateOrCreate([
                        'item_id' => $newItemModel -> id,
                        'sort' => $sort1,
                    ],[
                        'name' => $option['name'],
                        'value' => $option['value'],
                        'is_other' => $option['is_other']??false,
                    ]);
                    $optionIds[] = $newOptionModel -> id;
                }
                ChSmartQuestionnaireOption::query() -> where('item_id',$newItemModel -> id) -> whereNotIn('id',$optionIds) -> delete();
            }
        }
        //删除没用的
        self::query() -> where('questionnaire_id',$questionnaireId) -> whereNotIn('id',$itemIds) -> delete();
    }
}
